﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using OOAD_PROJEKAT;

namespace DAL
{
    partial class DAL
    {
        public class SastanakDAO : IDaoCrud<Sastanak>
        {

            protected MySqlCommand c;

            public long create(Sastanak s)
            {
                try
                {

                    c = new MySqlCommand();
                    c.CommandText = "insert into sastanci values (@idSastanka, @vrijemeSastanka, @razlogSastanka)";
                    c.Parameters.AddWithValue("@idSastanak", "");
                    c.Parameters.AddWithValue("@vrijemeSastanka", s.VrijemeSastanka);
                    c.Parameters.AddWithValue("@razlogSastanka", s.RazlogSastanka);

                    c.Connection = con;
                    c.ExecuteNonQuery();

                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Sastanak> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from sastanci", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Sastanak> sastanci = new List<Sastanak>();
                    while (r.Read())
                        sastanci.Add(new Sastanak(r.GetInt32("idSastanka"), r.GetString("razlogSastanka"), r.GetDateTime("vrijemeSastanka")));

                    r.Close();
                    return sastanci;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }


            public Sastanak read(Sastanak s)
            {
                try
                {
                    string commandStr = "select * from sastanci where idSastanka=" + s.Id;
                    c = new MySqlCommand(commandStr, con);
                    MySqlDataReader r = c.ExecuteReader();
                    Sastanak pr = null;
                    if (r.Read())
                        pr = new Sastanak(r.GetInt32("idSastanka"), r.GetString("razlogSastanka"), r.GetDateTime("vrijemeSastanka"));
                    r.Close();
                    return pr;
                }

                catch (Exception e)
                {
                    throw e;
                }
            }

            public Sastanak update(Sastanak s)
            {
                try
                {
                    String commandStr = "update sastanci set razlgoSastanka = ?a, ";
                    commandStr += "vrijemeSastanka = ?b ";
                    commandStr += "where idSastanka=" + s.Id;

                    c = new MySqlCommand(commandStr, con);
                    c.Parameters.AddWithValue("?a", s.RazlogSastanka);
                    c.Parameters.AddWithValue("?b", s.VrijemeSastanka);

                    c.ExecuteNonQuery();
                    return read(s);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Sastanak s)
            {
                try
                {
                    string commandStr = "delete from sastanci where idSastanka=" + s.Id;

                    c = new MySqlCommand(commandStr, con);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }

            }

            public Sastanak getById(int id)
            {
                try
                {
                    Sastanak s = new Sastanak(id, "", DateTime.Now);
                    return read(s);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Sastanak> getByExample(string name, string value)
            {
                throw new NotImplementedException();
            }

        }
    }
}